package lockDemo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* @author jie
* @version 创建时间：2018年8月3日 下午6:02:40
* 类说明 给异步加锁
 *
 * 读写锁
 *
 * 读可以并发。
 * 读写，之间，同步执行。
 * 写 同步。
 *
*/
public class ReadWriteLockDemo {
	private static ReentrantReadWriteLock lockReadWrite = new ReentrantReadWriteLock();
	private static ReentrantReadWriteLock.ReadLock readLock = lockReadWrite.readLock();
	private static ReentrantReadWriteLock.WriteLock writeLock = lockReadWrite.writeLock();
	private static final Lock lock = new ReentrantLock();
	public static void main(String[] args) {
			Object object = new Object();
		for (int i = 0; i < 10; i++) {
			Method(i,object);
		}
	}

	private static void Method(int i,Object obj) {
//		lock.writeLock().lock();
//		lock.lock();
		try {
			System.out.println(i+"");
			Thread.sleep(1000);
			
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
//		 new Thread(new Runnable() {
//			@Override
//			public void run() {
////				synchronized (obj) {
//				try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				System.out.println(i);
//				lock.unlock();
////				lock.writeLock().unlock();
////			}
//			}
//		}).start();
		
	}
}
